package 算法回顾;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-09-25
 **/

public class No162寻找峰值 {

    public int findPeakElement(int[] nums) {

        if (nums.length == 1) {
            return 0;
        }
        if (nums.length == 2) {
            return nums[0] > nums[1] ? 0 : 1;
        }

        int left = 0;
        int right = nums.length - 1;

        while (left < right) {
            int mid = (right - left) / 2 + left;
            if (mid == 0 || mid == nums.length - 1) {
                return mid;
            }
            int lastNum = nums[mid - 1];
            int thisNum = nums[mid];
            int nextNum = nums[mid + 1];
            if (nextNum > thisNum) {
                left = mid + 1;
            } else if (lastNum > thisNum) {
                right = mid;
            } else {
                return mid;
            }
        }

        return left;
    }

    public static void main(String[] args) {
        No162寻找峰值 n = new No162寻找峰值();
        int[] arr = {1,2,4};
        int result = n.findPeakElement(arr);
        System.out.println(result);
    }

}
